package action.user;

import java.text.SimpleDateFormat;
import java.util.List;

import model.HeadImg;
import model.Page;
import model.PassModify;
import model.User;

import DAO.UserDAO;
import DAO.DAOImple.UserDAOImple;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private User user;
	List<User> users;
	private PassModify pm;
	private Page page;
	
	private List<HeadImg> headImgs;
	private HeadImg headImg;
	
	public String execute() throws Exception {

		return SUCCESS;
	}

	public String addMember() {
		ActionContext act = ActionContext.getContext();
		UserDAO userdao = new UserDAOImple();
		
		int result = userdao.addUser(user);
		
		if(result == 0) {
			act.put("addwrong", "0");
			//增加失败！
			return ERROR;
		}else if (result == 1) {
			act.put("addwrong", "1");
			//用户名已存在
			return ERROR;
		}else {		
			return SUCCESS;
		}
	}
	//返回队员列表
	public String getMemberList() {
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_User where state = 0",page.getCurrentPage(),50);  //获得分页对象
		
		UserDAO userdao = new UserDAOImple();
		users = userdao.getUserList(page);
		
		return SUCCESS;
	}
	
	//删除队员  并不真正删除 只是将队员的状态改为退出
	public String deleteMember() { 
		UserDAO userdao = new UserDAOImple();
		userdao.deleteUser(user);

		page = new Page();
		page.setCurrentPage(1);
		
		page = page.getPage("T_User where state = 0",page.getCurrentPage(),50);
		users = userdao.getUserList(page);
		return SUCCESS;
	}
	
	//修改密码
	public String passModify() {
		ActionContext act = ActionContext.getContext();
		user = (User) act.getSession().get("user");
		UserDAO userdao = new UserDAOImple();
		if(pm.getNewPass().length() < 6) {
			act.put("addwrong", "2");
			//新密码长度不得少于6位！
		}else if(!(pm.getNewPass().equals(pm.getComfirmPass()))) {
			act.put("addwrong", "3");
			//两次密码不一致！
		}else {
			int result = userdao.PassModify(pm,user);
				
			if(result == 0) {
				act.put("addwrong", "0");
				//修改密码失败！
			}else if (result == 1) {
				act.put("addwrong", "1");
				//当前密码不正确！
			}else {		
				act.put("addwrong", "4");
				//修改密码成功！
			}
		}
		return SUCCESS;
	}
	
	//返回队员信息
	public String getMemberInfo() {
		UserDAO userdao = new UserDAOImple();
		user = userdao.getUser(user.getId());  //根据队员id返回队员实例
		return INPUT;
	}
	
	//修改信息
	public String infoModify() {
		ActionContext act = ActionContext.getContext();
		UserDAO userdao = new UserDAOImple();
		int result = userdao.infoModify(user);
		
		if(result == 0) {
			act.put("addwrong", "0");
			//修改信息失败！
			return INPUT;
		}else if (result == 1) {
			act.put("addwrong", "1");
			//用户名已存在！
			return INPUT;
		}else {		
			user = userdao.getUser(user.getId());
			return SUCCESS;
		}
	}
	
	//修改头像
	public String getMemberHeadImgList() {
		UserDAO userdao = new UserDAOImple();
		headImgs = userdao.getHeadImgList(user.getId());
		return SUCCESS;
	}
	
	public String modifyImg() {
		UserDAO userdao = new UserDAOImple();
		int result = userdao.modifyImg(user.getId(), user.getHeadImg().getId());
		user = userdao.getUser(user.getId());
		return SUCCESS;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public PassModify getPm() {
		return pm;
	}

	public void setPm(PassModify pm) {
		this.pm = pm;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<HeadImg> getHeadImgs() {
		return headImgs;
	}

	public void setHeadImgs(List<HeadImg> headImgs) {
		this.headImgs = headImgs;
	}
	public HeadImg getHeadImg() {
		return headImg;
	}

	public void setHeadImg(HeadImg headImg) {
		this.headImg = headImg;
	}


}
